//
//  BGHomePageTopGoodsCell.m
//  topSilderBar
//
//  Created by huangzhibiao on 16/7/7.
//  Copyright © 2016年 Biao. All rights reserved.
//

#import "BGHomePageTopGoodsCell.h"
#import "NXAWorkTableViewCell.h"
#import "NXAPickerView.h"


@interface BGHomePageTopGoodsCell ()<UITableViewDelegate,UITableViewDataSource>
{
    //__block BOOL _selectedAll;  //!<此字段为真，则选中所有cell
    
//    __block NXATimeSegmentModel *_timeSegmentModel;   //!<一个时段的model
//    __block NXANonIssueModel *_nonIssueModel;   //!<一天的model
}

@property(nonatomic,strong)NSMutableArray *seletedStateArr;  //!<记录每一个cell的选中状态

@property(nonatomic,assign)BOOL selectedAll;  //!<此字段为真，则选中所有cell


//@property(nonatomic,strong)NXASingleDataManager *dataManager;    //!<

@end

@implementation BGHomePageTopGoodsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
        
    [self initComponents];
}

//- (void)layoutSubviews
//{
//    [self.contentView addSubview:[self footerView]];
//}



#pragma mark - 初始化

- (void)initData
{
    
}


- (void)initComponents
{
    NXANonIssueContentView *view = [[NXANonIssueContentView alloc]initWithFrame:CGRectMake(0, 0, CURRENT_SCREEN_WIDTH, 40)];
    
    [view setSelectedAllCallBack:^(BOOL b) {
        _selectedAll = b;
        if (_selectedAll) {
            //把数组里相应的状态也全置为1
            for (int i = 0; i < self.seletedStateArr.count; i++) {
                [self.seletedStateArr replaceObjectAtIndex:i withObject:@(1)];
            }
        }else{
            for (int i = 0; i < self.seletedStateArr.count; i++) {
                [self.seletedStateArr replaceObjectAtIndex:i withObject:@(0)];
            }
        }
        
        //刷新表格，选中所有cell
        [self.tableView reloadData];
    }];
    view.dataSourceArr = @[@"时段", @"时价(元/时)", @"预约人数"];
    [self addSubview:view];
    
    
    self.selectedAll = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   // self.tableView.bounces = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"NXAWorkTableViewCell" bundle:nil] forCellReuseIdentifier:@"NXAWorkTableViewCell"];
    
}


#pragma mark - EventsHandle

- (void)clickToPopPickView:(UIButton *)sender
{
    NXAPickerView *pickView = [[NSClassFromString(@"NXAPickerView") alloc]initWithFrame:CGRectZero];
    pickView.column = 3;
    pickView.center = self.center;
    pickView.bounds = self.bounds;
    [self addSubview:pickView];
    
    [pickView setTransValueCallBack:^(NSMutableArray *valueArr) {
        NXATimeSegmentModel *model = [[NXATimeSegmentModel alloc]init];
        model = [[NXATimeSegmentModel alloc]init];
        model.learn_type = valueArr[0];   //车型
        model.subject_type = valueArr[1];   //科目
        model.check_number = valueArr[2];   //人数
    }];
}




#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NXAWorkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NXAWorkTableViewCell"];
    cell.segmentModel = self.dataSource[indexPath.row];
    if (self.selectedAll) {
        //选中所有cell
        cell.areadySelected = YES;
    }else{
        //取消选中
        cell.areadySelected = NO;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}



//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    if (self.dataSource.count == 0) {
//        return nil;
//    }
////    return [self footerView];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.dataSource.count == 0) {
        return 1;
    }
    
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSNumber *number = self.seletedStateArr[indexPath.row];
    
    if ([number integerValue] == 0) {
        [self.seletedStateArr replaceObjectAtIndex:indexPath.row withObject:@(1)];
    }else{
        [self.seletedStateArr replaceObjectAtIndex:indexPath.row withObject:@(0)];
    }
    BOOL b = [self.seletedStateArr[indexPath.row] boolValue];
//    DLog(@"%d",b);
    NXAWorkTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.areadySelected = b;
    
}



#pragma mark - 网络请求

/**
 获取未公布班表日期对应时段(访问地市)
 
 @param coachId 教练编号
 @param date 日期
 */
- (void)fetchTimeSegmentWithCoachId:(NSString *)coachId date:(NSString *)date areaUrl:(NSString *)areaUrl
{
//    NXANetworkingDataManager *netManager = [NXANetworkingDataManager shareManager];
//    NSString *urlString = [areaUrl stringByAppendingString:@"app/GetDataInfo"];
//    
//    NSDictionary *tempDic = @{@"CoachId":coachId,
//                              @"Date":date,
//                              @"PlatformType":@"IOS",
//                              @"RoleType":@"c",
//                              @"PhoneMode":kPhoneMode};
//    NSString *jsonStr = [NSDictionary dictionaryToJson:tempDic];
//    NSDictionary *parameDic = @{@"CommandCode":@"GetDatePeriods",
//                                @"Marker":@(1483514647244),
//                                @"TransferData":jsonStr};
//    NSDictionary *parameDic2 = @{@"entity":parameDic};
//    
//    [netManager postDataWithUrl:urlString parameters:parameDic2 success:^(id json) {
//        
//        
//        NSDictionary *resultDic = (NSDictionary *)json;
//        DLog(@"%@",resultDic);
//        //NSString *code = resultDic[@"ErrorCode"];
//        
//        NSArray *arr = (NSArray *)[NXAPublicClass getDicString:resultDic[@"ResultInfo"]];
//        DLog(@"%@",arr);
//        self.dataSource = [[NSMutableArray<NXATimeSegmentModel> alloc]init];
//        for (NSDictionary *segmentDic in arr) {
//            NXATimeSegmentModel *segmentModel = [[NXATimeSegmentModel alloc]initWithDictionary:segmentDic error:nil];
//            segmentModel.fee = @"100";
//            [self.dataSource addObject:segmentModel];
//        }
//        self.nonIssueModel.segmentsArr = self.dataSource;
//        
//        [self.tableView reloadData];
//        
//    } failure:^(NSError *error) {
//        
//    }];
}





#pragma mark - other


#pragma mark - setter

- (void)setDate:(NSString *)date
{
    _date = date;
    
    self.nonIssueModel.dateStr = date;
    
    //获取未公布班表一天的不同时段
//    [self fetchTimeSegmentWithCoachId:self.dataManager.loginModel.ResId date:date areaUrl:self.dataManager.loginModel.serverUrl];
}




#pragma mark - 懒加载

//- (NXASingleDataManager *)dataManager
//{
//    if (_dataManager == nil) {
//        _dataManager = [NXASingleDataManager shareManager];
//    }
//    return _dataManager;
//}

- (NSMutableArray *)seletedStateArr
{
    if (!_seletedStateArr) {
        NSMutableArray *arrM = [[NSMutableArray alloc]init];
        for (int i = 0; i < self.dataSource.count; i++) {
            [arrM addObject:@(0)];
        }
        _seletedStateArr = [[NSMutableArray alloc]initWithArray:arrM];
    }
    return _seletedStateArr;
}

//tableView footer视图
//- (UIView *)footerView
//{
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, NXAWidth, 100)];
//    view.backgroundColor = RGBA(235, 235, 235, 1);
//    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, NXAWidth, 20)];
//    label.text = @"注:人数为0代表该时段不开放培训";
//    label.font = [UIFont systemFontOfSize:14];
//    label.textColor = ThemeColor;
//    label.adjustsFontSizeToFitWidth = YES;
//    [view addSubview:label];
//    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 40, NXAWidth, 40)];
//    btn.layer.cornerRadius = 5;
//    btn.layer.masksToBounds = YES;
//    [btn setBackgroundColor:ThemeColor];
//    [btn setTintColor:[UIColor whiteColor]];
//    [btn setTitle:@"设置" forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(clickToPopPickView:) forControlEvents:UIControlEventTouchUpInside];
//    [view addSubview:btn];
    
//    return view;
//}


@end
